<template>
  <div class='article-container'>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <my-breadcrumb>评论管理</my-breadcrumb>
      </div>
      <!-- 表格 -->
       <el-table
        :data="comments"
        border
        style="width: 100%">
         <el-table-column label="文章标题"  prop="title"></el-table-column>
         <el-table-column label="总评论数"  prop="total_comment_count"></el-table-column>
         <el-table-column label="粉丝评论数"  prop="fans_comment_count"></el-table-column>
         <el-table-column label="评论状态"> <!-- prop="comment_status" -->
           <template slot-scope="scope">
              <el-tag v-if="scope.row.comment_status" type="success">正常</el-tag>
              <el-tag v-else type="error">关闭</el-tag>
        </template>
         </el-table-column>
         <el-table-column label="操作" >
        <template slot-scope="scope">
             <el-button size="mini" v-if="scope.row.comment_status" type="danger" @click="hToggleStatus(scope.row)">关闭</el-button>
             <el-button size="mini" v-else type="primary" @click="hToggleStatus(scope.row)">打开</el-button>
        </template>
        </el-table-column>
       </el-table>
       <!-- 分页 -->
       <el-pagination
         background
         layout="prev, pager, next"
         :total="total_count"
         style="margin-top:35px"
         @current-change="hPag"
         >
        </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      page: 1, // 当前的页数
      total_count: 0, // 总页数
      comments: [] // 数据
    }
  },
  created () {
    this.loadComments()
  },
  methods: {
    // 获取数据函数
    async loadComments () {
      const res = await this.$http({
        url: '/mp/v1_0/articles',
        method: 'GET',
        params: {
          page: this.page,
          response_type: 'comment'
        }
      })
      // 请求成功后
      console.log(res)
      this.comments = res.data.data.results
      this.total_count = res.data.data.total_count
    },
    // 页码改变处理函数
    hPag (curpage) {
      this.page = curpage // 更新页码
      this.loadComments() // 重发请求
    },
    // 切换状态处理函数
    async hToggleStatus (row) {
      try {
        const res = await this.$http({
          url: '/mp/v1_0/comments/status',
          method: 'PUT',
          params: {
            article_id: row.id.toString() // 大数，要加toString
          },
          data: {
            allow_comment: !row.comment_status // 取反
          }
        })
        console.log(res)
        this.$message.success('操作成功')
        row.comment_status = !row.comment_status // 局部更新
      } catch (err) {
        console.log(err)
        this.$message.error('操作失败')
      }
    }
  }
}
</script>

<style>

</style>
